/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.controller;

import com.yanlight.app.request.OrderRequest;
import com.yanlight.app.service.OrderService;
import com.yanlight.support.http.Response;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.LogUtil;
import com.yanlight.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author haolw
 * @time 2017/08/18
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/list")
    public Response list(OrderRequest request) {
        return ResponseUtil.success(orderService.list(request));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse httpResponse, OrderRequest request) {
        OutputStream outputStream = null;
        try {
            outputStream = httpResponse.getOutputStream();
            String fileName = "order_".concat(DateTimeUtil.getCompactMillNowTime()) + ".xlsx";
            httpResponse.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        orderService.export(outputStream, request);
    }

    @PostMapping("/drawback")
    @ResponseBody
    public String drawback(String orderId) {
        Response drawback = orderService.drawback(orderId);
        return "success";
    }

    @PostMapping("/drawbackResponse/{orderId}")
    @ResponseBody
    public void drawbackResponse(@PathVariable String orderId) {
        LogUtil.showLog(orderId);
    }
}
